<!DOCTYPE html>
<html lang="en">

	<head>
		<title>Matrix Admin</title>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<link rel="stylesheet" href="/css/bootstrap.min.css" />
		<link rel="stylesheet" href="/css/bootstrap-responsive.min.css" />
		<link rel="stylesheet" href="/css/uniform.css" />
		<link rel="stylesheet" href="/css/select2.css" />
		<link rel="stylesheet" href="/css/matrix-style.css" />
		<link rel="stylesheet" href="/css/matrix-media.css" />
		<link href="/font-awesome/css/font-awesome.css" rel="stylesheet" />
	</head>

	<body>

		<!--Header-part-->
		<div id="header">
			<h1><a href="dashboard.html">Matrix Admin</a></h1>
		</div>
		<!--close-Header-part-->

		<!--top-Header-menu-->
		<div id="user-nav" class="navbar navbar-inverse">
			<ul class="nav">
				<li class="dropdown" id="profile-messages">
					<a title="" href="#" data-toggle="dropdown" data-target="#profile-messages" class="dropdown-toggle"><i class="icon icon-user"></i> <span class="text">${user.user_name}</span><b class="caret"></b></a>
					<ul class="dropdown-menu">
						<li>
							<a href="#"><i class="icon-user"></i> My Profile</a>
						</li>
						<li class="divider"></li>
						<li>
							<a href="#"><i class="icon-check"></i> My Tasks</a>
						</li>
						<li class="divider"></li>
						<li>
							<a href="/login/logout"><i class="icon-key"></i> Log Out</a>
						</li>
					</ul>
				</li>
				<li class="dropdown" id="menu-messages">
					<a href="#" data-toggle="dropdown" data-target="#menu-messages" class="dropdown-toggle"><i class="icon icon-envelope"></i> <span class="text">Messages</span> <span class="label label-important">5</span> <b class="caret"></b></a>
					<ul class="dropdown-menu">
						<li>
							<a class="sAdd" title="" href="#"><i class="icon-plus"></i> new message</a>
						</li>
						<li class="divider"></li>
						<li>
							<a class="sInbox" title="" href="#"><i class="icon-envelope"></i> inbox</a>
						</li>
						<li class="divider"></li>
						<li>
							<a class="sOutbox" title="" href="#"><i class="icon-arrow-up"></i> outbox</a>
						</li>
						<li class="divider"></li>
						<li>
							<a class="sTrash" title="" href="#"><i class="icon-trash"></i> trash</a>
						</li>
					</ul>
				</li>
				<li class="">
					<a title="" href="#"><i class="icon icon-cog"></i> <span class="text">Settings</span></a>
				</li>
				<li class="">
					<a title="" href="/login/logout"><i class="icon icon-share-alt"></i> <span class="text">Logout</span></a>
				</li>
			</ul>
		</div>

		<!--start-top-serch-->
		<div id="search">
			<input type="text" placeholder="Search here..." />
			<button type="submit" class="tip-bottom" title="Search"><i class="icon-search icon-white"></i></button>
		</div>
		<!--close-top-serch-->

		<!--sidebar-menu-->

		<div id="sidebar">
			<a href="#" class="visible-phone"><i class="icon icon-th"></i>Tables</a>
			<ul>
				<li>
					<a href="/project/list"><i class="icon icon-home"></i> <span>项目管理</span></a>
				</li>
				<li class="active">
					<a href="/confsync/configServiceList"><i class="icon icon-home"></i> <span>配置管理</span></a>
				</li>
			</ul>
		</div>
		<div id="content">
			<div id="content-header">
				<div id="breadcrumb">
					<a href="/login/index" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a>
					<a onclick="history.go(-1);" style="cursor:pointer">配置管理</a>
					<a href="#" class="current">文件列表</a>
				</div>
				<!--<h1>Tables</h1>-->
			</div>
			<div class="container-fluid">
				<hr>
				<a class="btn btn-success" onclick="createConfigFile()">新建配置文件</a>
				<a class="btn btn-success"  href="/server/list/${configServiceId}">主机管理</a>
				<div class="row-fluid">
					<div class="span12">
						<div class="widget-box">
							<div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
								<h5>文件列表</h5>
							</div>
							<div class="widget-content nopadding">
								<table class="table table-bordered table-striped">
									<thead>
										<tr>
											<th>文件名称</th>
											<th>文件路径</th>
											<th style="width: 250px;">操作</th>
										</tr>
									</thead>
									<tbody>
										<#list configs as config >
											<tr class="odd gradeX">
												<td>${config.config_name}</td>
												<td>${config.file_path}</td>
												<td>
													<a class="btn btn-success btn" onclick="editConfigFile(${config.config_id})">编辑</a>
													<a class="btn btn-danger  btn" onclick="deleteConfigFile(${config.config_id})">删除</a>
													<div class="btn-group">
										              <button data-toggle="dropdown" class="btn dropdown-toggle">请选择主机 <span class="caret"></span></button>
										              <ul class="dropdown-menu">
										              	<#list hosts as host>
										              		<li><a onclick="downConfig(${config.config_id},${host.server_id})">${host.server_name}</a></li>
										              	</#list>
										              </ul>
										            </div>
												</td>
											</tr>
										</#list>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
			<!--Footer-part-->
			<div class="row-fluid">
				<div id="footer" class="span12"> 2013 &copy; Matrix Admin. Brought to you by
					<a href="http://themedesigner.in/">Themedesigner.in</a>
				</div>
			</div>
			<!--end-Footer-part-->
			<script src="/js/jquery.min.js"></script>
			<script src="/js/jquery.ui.custom.js"></script>
			<script src="/js/bootstrap.min.js"></script>
			<script src="/js/jquery.uniform.js"></script>
			<script src="/js/select2.min.js"></script>
			<script src="/js/jquery.dataTables.min.js"></script>
			<script src="/js/matrix.js"></script>
			<script src="/js/matrix.tables.js"></script>
			<script src="/layer/layer.js"></script>
			<script type="text/javascript">
				// 删除项目
				function deleteConfigFile(configId){
					//询问框
					layer.confirm('确定删除当前配置文件吗？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
//					  document.location.href = "/confsync/deleteFile?configServiceId="+configServiceId;
					  	var param = {};
						param['configServiceId'] = ${configServiceId};
						param['configId'] = configId;
						
						$.ajax({
							type: "POST",
							url: "/confsync/deleteFile",
							data: param,
							success: function(data) {
								location.reload();
							}
						});
					});
				}
				
				function createConfigFile(){
					
					layer.open({
					  type: 2,
					  area: ['700px', '530px'],
					  title:'文件编辑',
					  fixed: true, //不固定
					  maxmin: false,
					  content: '/confsync/createFile',
					  btn:['保存','取消'],
					  yes:function(index, layero){
					  	var body = layer.getChildFrame('body', index);
						var iframeWin = window[layero.find('iframe')[0]['name']];
						var txt = iframeWin.getFileContent();
						var fileName = iframeWin.getFileName();
						var filePath = iframeWin.getFilePath();
						
						if(fileName == '' || filePath == ''){
							alert("请输入文件名称或文件路径!");
							return false;
						}
						saveConfigFile(txt,fileName,-1,filePath);
					  }
					});
				}
				
				function saveConfigFile(content,fileName,configId,filePath){
					
					var param = {};
					param['configServiceId'] = ${configServiceId};
					param['content'] = content;
					param['configFileName'] = fileName;
					param['configId'] = configId;
					param['filePath'] = filePath;
					
					$.ajax({
						type: "POST",
						url: "/confsync/saveConfigFile",
						data: param,
						success: function(data) {
							location.reload();
						}
					});
				}
				
				function editConfigFile(configId){
					
					layer.open({
					  type: 2,
					  area: ['700px', '530px'],
					  title:'文件编辑',
					  fixed: true, //不固定
					  maxmin: false,
					  content: '/confsync/getConfigFile?configId='+configId,
					  btn:['保存','取消'],
					  yes:function(index, layero){
					  	
					  	var body = layer.getChildFrame('body', index);
						var iframeWin = window[layero.find('iframe')[0]['name']];
						var txt = iframeWin.getFileContent();
						var fileName = iframeWin.getFileName();
						var configId = iframeWin.getConfigId();
						var filePath = iframeWin.getFilePath();
						
						if(fileName == '' || filePath == ''){
							alert("请输入文件名称或文件路径!");
							return false;
						}
						
						saveConfigFile(txt,fileName,configId,filePath);
					  }
					});
				}
				
				// 对主机进行管理
				function serverList(){
				
					layer.open({
					  type: 2,
					  area: ['700px', '530px'],
					  title:'文件编辑',
					  fixed: true, //不固定
					  maxmin: false,
					  content: '/server/list',
					  btn:['保存','取消'],
					  yes:function(index, layero){
					  	var body = layer.getChildFrame('body', index);
						var iframeWin = window[layero.find('iframe')[0]['name']];
						var txt = iframeWin.getFileContent();
					  }
					});
				}
				
				// 下发配置文件
				function downConfig(configId,serverId){
					
					if(serverId == '-1'){ return false; }
					
					layer.confirm('确定向当前选择主机同步配置吗？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
						layer.open({
						  type: 2,
						  area: ['700px', '530px'],
						  title:'控制台',
						  fixed: true, //不固定
						  maxmin: false,
						  content: '/server/console'
						});
					});
				}
				
				function downOperate(){
					
					var param = {};
					param['configId'] = configId;
					param['hosts'] = serverId+'';
					
					$.ajax({
						type: "POST",
						url: "/confsync/sync",
						data: param,
						success: function(data) {
						}
					});
				}
			</script>
	</body>

</html>